package 算法.练习;

import sun.nio.ch.ThreadPool;

import java.util.concurrent.*;

/**
 * @author:谢君臣
 * @Date:2021/7/6-15:47
 * @version:1.0
 * @function:
 */
public class TestThreadPool {
    public static void main(String[] args) {

        FutureTask<Object> task = new FutureTask<>((Callable<Object>)()->{return new Object();});
        FutureTask<Object> task1 = new FutureTask<>(Object::new);
        Object o = null;
        try {
            o = task.get(); //返回Callable里call（）方法的返回值，调用这个方法会导致程序阻塞，必须等到子线程结束后才会得到返回值
            task.isDone(); //若Callable任务完成，返回True
            task.isCancelled(); //如果在Callable任务正常完成前被取消，返回True
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(o);

        ExecutorService poolExecutor = Executors.newFixedThreadPool(10);
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);

        poolExecutor.submit(()->{ System.out.println("asdfasd"); });
        poolExecutor.execute(()->{
            System.out.println(Thread.currentThread());
        });
        try {
            System.out.println(poolExecutor.submit(new MyThread3()).get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}
class MyThread1 extends Thread{
    @Override
    public void run() {
        super.run();
    }
}
class MyThread2 implements Runnable { //实现Runnable接口
    @Override
    public void run() {
    }

}
class MyThread3 implements Callable { //实现Runnable接口
    @Override
    public Object call() throws Exception {
        return null;
    }
}
